package cn.baiweigang.uu.service.linearswap;

import cn.baiweigang.uu.enums.PeriodEnum;
import cn.baiweigang.uu.model.KLineInfo;
import cn.baiweigang.uu.model.StrategyInfo;
import cn.baiweigang.uu.strategy.linearswap.KLineList;
import cn.baiweigang.uu.utils.LogUtil;
import org.springframework.stereotype.Service;

/**
 * 以分数的形式 返回当前趋势
 * 返回分数范围 -100到+100之间
 */
@Service
public class UpOrDownService {

    /**
     * 根据小时级柱状图，判断12小时内的趋势
     *
     * @param kLineList
     * @return
     */
    public int hour1(KLineList<KLineInfo> kLineList) {

        return 0;
    }

    public int hour4(KLineList<KLineInfo> kLineList) {

        return 0;
    }

    public int day1(KLineList<KLineInfo> kLineList) {

        return 0;
    }

    /**
     * 接下来要上升的概率
     *
     * @param tmpkLineList
     * @return
     */
    public int min5Up(KLineList<KLineInfo> tmpkLineList) {
        int sum = 0;
        //取前5个进行判断
        KLineList<KLineInfo> kLineList = tmpkLineList.subList(0, 5);
        kLineList.refreshAll();

        //目前根据5个判断
        if (kLineList == null || kLineList.getPeriodEnum() != PeriodEnum.MIN5 ||
                kLineList.size() != 5) {
            LogUtil.logError("KLineList数据格式不符合要求 ");
            return 0;
        }
        //先统计跌的个数
        int down = 0;

        double changeSum = 0.00;
        for (int i = 0; i < kLineList.size(); i++) {
            if (kLineList.get(i).getChangeRate().doubleValue() < -0.009) {
                down++;
            }
            changeSum = changeSum + kLineList.get(i).getChangeRate().abs().doubleValue();
        }
        //一个下跌 +10分 最多50分
        sum = down * 10;
        LogUtil.logDebug("down的K线有" + down + "个，得分=" + sum);

        //最后1个比前几个低 最多50分 后面几个都要持续涨或跌
        int lastDownNum = lastKLessZero(kLineList);
        LogUtil.logDebug("最后持续比前" + kLineList.getLast().getLessPreCount() + "个都低 ");
        switch (kLineList.getLast().getLessPreCount()) {
            case 4:
                //最后4个都得是跌的
                if (lastDownNum >= 4) {
                    sum += 50;
                } else if (lastDownNum >= 3) {
                    sum += 40;
                } else {
                    sum += 30;
                }
                break;
            case 3:
                //最后3个都得是跌的
                if (lastDownNum >= 3) {
                    sum += 30;
                } else {
                    sum += 15;
                }
                break;
            case 2:
                //最后2个都得是跌的
                if (lastDownNum >= 2) {
                    sum += 10;
                } else {
                    sum += 5;
                }
                break;
            case 1:
                sum += 5;
                break;
            default:
        }
        LogUtil.logDebug("连续低的得分增加后=" + sum);


        //涨跌率 绝对值之和小于0.3 则无趋势
        if (changeSum < 0.2) {
            return 0;
        }
        return sum;
    }


    /**
     * 接下来要下降的概率
     *
     * @param tmpkLineList
     * @return
     */
    public int min5Down(KLineList<KLineInfo> tmpkLineList) {
        int sum = 0;
        //取前5个进行判断
        KLineList<KLineInfo> kLineList = tmpkLineList.subList(0, 5);
        kLineList.refreshAll();

//        LogUtil.logInfo(JSONObject.toJSONString(kLineList));
        //目前根据5个判断
        if (kLineList == null || kLineList.getPeriodEnum() != PeriodEnum.MIN5 ||
                kLineList.size() != 5) {
            LogUtil.logError("KLineList数据格式不符合要求 ");
            return 0;
        }
        //先统计涨跌K线个数
        int up = 0;

        double changeSum = 0.00;
        for (int i = 0; i < kLineList.size(); i++) {
            if (kLineList.get(i).getChangeRate().doubleValue() > 0.009) {
                up++;
            }
            changeSum = changeSum + kLineList.get(i).getChangeRate().abs().doubleValue();
        }
        //一个下跌 +10分 最多50分
        sum = up * 10;
        LogUtil.logDebug("up的K线有" + up + "个，得分=" + sum);
        //最后1个比前几个低 最多50分
        int lastUpNum = lastKGreaterZero(kLineList);
        LogUtil.logDebug("最后持续比前" + kLineList.getLast().getGreaterPreCount() + "个都高 ");
        switch (kLineList.getLast().getGreaterPreCount()) {
            case 4:
                if (lastUpNum >= 4) {
                    sum += 50;
                } else if (lastUpNum >= 3) {
                    sum += 40;
                } else {
                    sum += 30;
                }
                break;
            case 3:
                if (lastUpNum >= 3) {
                    sum += 30;
                } else {
                    sum += 15;
                }
                break;
            case 2:
                if (lastUpNum >= 2) {
                    sum += 10;
                } else {
                    sum += 5;
                }
                break;
            case 1:
                sum += 5;
                break;
            default:
        }
        LogUtil.logDebug("连续高的得分增加后=" + sum);

        //涨跌率 绝对值之和小于0.3 则无趋势
        if (changeSum < 0.2) {
            return 0;
        }
        return sum;
    }

    public void updateScore(StrategyInfo strategyInfo) {
        strategyInfo.setUpScore(min5Up(strategyInfo.getKLineList()));
        strategyInfo.setDownScore(min5Down(strategyInfo.getKLineList()));
    }

    private int lastKLessZero(KLineList<KLineInfo> kLineList) {
        int num = 0;
        for (int i = 4; i >= 0; i--) {
            if (kLineList.get(i).getChangeRate().doubleValue() < 0) {
                num++;
            } else {
                break;
            }
        }
        return num;
    }

    private int lastKGreaterZero(KLineList<KLineInfo> kLineList) {
        int num = 0;
        for (int i = 4; i >= 0; i--) {
            if (kLineList.get(i).getChangeRate().doubleValue() > 0) {
                num++;
            } else {
                break;
            }
        }
        return num;
    }

}
